﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Text;
using Saturn.App.Common;

namespace Saturn.App
{

    public class CommonConfig
    {
        private const string m_configFile = @"./Program Files/Saturn.App/Config/SystemConfig.xml";
        private const string m_configFileUrl = @"./Program Files/Saturn.App/Config";
        //private const string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        public Dictionary<string, string> m_list = new Dictionary<string, string>();

        private CommonConfig()
        {
            Select();
        }
        private static CommonConfig m_serverCfg = null;
        public static CommonConfig Instance()
        {
            if (m_serverCfg == null)
            {
                m_serverCfg = new CommonConfig();
            }
            return m_serverCfg;
        }

        public bool Select()
        {
            try
            {
                m_list.Clear();

                //if (!Directory.Exists(m_configFileUrl))
                //{
                //    Directory.CreateDirectory(m_configFileUrl);
                //}
                //if (!File.Exists(m_configFile))
                //{
                //    XmlDocument xmldoc = new XmlDocument();
                //    xmldoc.InnerXml =
                //        "<?xml version='1.0' encoding='utf-8' ?><product-line></product-line>";
                //    //<add id="productline">L76</add><add id="location">纸箱</add><add id="lastnumber">0</add>
                //    #region
                //    XmlElement xe1 = xmldoc.CreateElement("add");//创建一个<Node>节点 
                //    XmlDocument doc = xe1.OwnerDocument;

                //    //XmlAttribute xe1 = xmldoc.CreateAttribute("productline");
                //    //添加逻辑id
                //    XmlAttribute attr = xmldoc.CreateAttribute("id");
                //    attr.Value = "productline";
                //    xe1.Attributes.SetNamedItem(attr);
                //    xmldoc.DocumentElement.AppendChild(xe1);
                //    #endregion
                //    //XmlAttribute xe1 = xmldoc.CreateAttribute("location");
                //    //XmlAttribute xe1 = xmldoc.CreateAttribute("lastnumber");


                //    //XmlDocument doc = xe1.OwnerDocument;


                //    xmldoc.Save(m_configFile);

                //}
                XmlDocument dom = new XmlDocument();
                dom.Load(m_configFile); //装载XML文档 
                //遍历所有节点 .
                foreach (XmlElement birthday in dom.DocumentElement.ChildNodes)
                {
                    m_list.Add(birthday.Attributes["id"].Value, birthday.InnerText);
                }

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public bool Update(string _param, string _value)
        {
            try
            {
                XmlDocument dom = new XmlDocument();
                dom.Load(m_configFile); //装载XML文档 
                if (!m_list.ContainsKey(_param))
                {
                    XmlElement xeId = dom.CreateElement("add");
                    XmlDocument doc = xeId.OwnerDocument;
                    XmlAttribute attr = doc.CreateAttribute("id");
                    attr.Value = _param;
                    xeId.Attributes.SetNamedItem(attr);

                    xeId.InnerText = _value;
                    dom.DocumentElement.AppendChild(xeId);
                    dom.Save(m_configFile);
                    m_list.Add(_param, _value);
                }
                else
                {
                    //遍历所有节点 .
                    foreach (XmlElement birthday in dom.DocumentElement.ChildNodes)
                    {
                        if (birthday.Attributes["id"].Value == _param)
                        {
                            birthday.InnerText = _value;
                        }
                    }
                    dom.Save(m_configFile);
                    m_list.Remove(_param);
                    m_list.Add(_param, _value);

                }
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }
       
    }
}
